package com.gsm.controller;

import com.gsm.entity.Result;
import com.gsm.po.SysMenu;
import com.gsm.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单(SysMenu)表控制层
 *
 * @author wjq
 * @since 2020-11-24 17:25:27
 */
@RestController
@RequestMapping("sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 根据用户id查询菜单列表
     * @param userId
     * @return
     */
    @GetMapping("menu/{userId}")
    public Result selectMenuByUserId(@PathVariable("userId") Long userId) {
        List<SysMenu> sysMenus = sysMenuService.selectMenuByUserId(userId);
        Result result = Result.success(sysMenus);
        return result;
    }

    /**
     * 新增
     */
    @PostMapping("sysMenu")
    public Result insertSysMenu(@RequestBody SysMenu sysMenu) {
        Result result = sysMenuService.insertSysMenu(sysMenu);
        return result;
    }

    /**
     * 根据 id 删除数据
     */
    @DeleteMapping("sysMenu/{menuId}")
    public Result deleteSysMenuByMenuId(@PathVariable("menuId") Long menuId) {
        Result result = sysMenuService.deleteSysMenuByMenuId(menuId);
        return result;
    }

    /**
     * 根据 id 修改单条数据
     */
    @PutMapping("sysMenu/{menuId}")
    public Result updateSysMenuByMenuId(@PathVariable("menuId") Long menuId, @RequestBody SysMenu sysMenu) {
        sysMenu.setMenuId(menuId);
        Result result = sysMenuService.updateSysMenuByMenuId(sysMenu);
        return result;
    }

    /**
     * 根据 id 查询单条数据
     */
    @GetMapping("sysMenu/{menuId}")
    public Result selectSysMenuByMenuId(@PathVariable("menuId") Long menuId) {
        Result result = sysMenuService.selectSysMenuByMenuId(menuId);
        return result;
    }

    /**
     * 根据 roleId 查询列表数据
     */
    @GetMapping("sysMenu/roleId/{roleId}")
    public Result selectSysMenuByRoleId(@PathVariable("roleId") Long roleId) {
        Result result = sysMenuService.selectSysMenuByRoleId(roleId);
        return result;
    }

    /**
     * 根据 roleId 分配菜单
     */
    @PutMapping("sysMenu/roleId/{roleId}")
    public Result updateSysMenuByRoleId(@PathVariable("roleId") Long roleId,@RequestBody List<Long> menuIds) {
        Result result = sysMenuService.updateSysMenuByRoleId(roleId,menuIds);
        return result;
    }

    /**
     * 根据条件查询 List
     */
    @GetMapping("sysMenuList")
    public Result selectSysMenuList(SysMenu sysMenu) {
        Result result = sysMenuService.selectSysMenuList(sysMenu);
        return result;
    }
}